package main

import (
	"local/middleware"
	"local/service_manager/web"

	"github.com/gin-gonic/gin"
)

func setupRouter() *gin.Engine {
	r := gin.Default()

	// 静态文件服务
	r.Static("/static", "./static")

	// 注册全局中间件
	middleware.All(r)

	// 不需要认证的页面
	r.StaticFile("/login", "./templates/login.html")
	r.StaticFile("/create-password.html", "./templates/create-password.html")

	// 需要认证的页面
	auth := r.Group("/")
	auth.Use(middleware.AuthRequired())
	{
		auth.StaticFile("/", "./templates/services.html")
		auth.StaticFile("/services", "./templates/services.html")
		auth.StaticFile("/config", "./templates/config.html")
	}

	// 登录相关API
	r.POST("/login", web.Login)
	r.POST("/create-password", web.CreatePassword)
	r.POST("/logout", web.Logout)

	// 配置API组，需要登录验证
	api := r.Group("/api")
	api.Use(middleware.AuthRequired())
	{
		api.GET("/config", web.ConfigGet)

		api.POST("/config", web.ConfigSet)

		// 密码修改API
		api.POST("/change-password", web.ChangePassword)

		// 服务管理API组
		serviceAPI := api.Group("/services")
		{
			// 列出所有服务
			serviceAPI.GET("/list", web.ServicesList)

			// 启动服务
			serviceAPI.POST("/:name/start", web.ServiceStart)

			// 停止服务
			serviceAPI.POST("/:name/stop", web.ServiceStop)

			// 获取服务状态
			serviceAPI.GET("/:name/status", web.ServiceStatus)
		}

		// 获取版本号
		api.GET("/version", web.GetVersion)
	}

	return r
}
